﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using VQData;
using VQWEB.administrator.sources;
using System.Data;
using System.Web.Routing;
using VQWEB.common;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for UsersHandler
    /// </summary>
    public class core_MembershipHandler : IHttpHandler, IRouteHandler
    {

        MembershipData MembershipData = new MembershipData();
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {         
            
            try
            {
                #region Roles
                string post_roles = context.Request["post_roles"];
                if (!string.IsNullOrEmpty(post_roles))
                {
                    core_user_role roles = serializer.Deserialize<core_user_role>(post_roles);
                    int Id = MembershipData.InsertOrUpdateRoles(roles);
                    if (post_roles.Contains("module"))
                    {
                        string[] modules = roles.module;
                        if (modules.Length > 0)
                            MembershipData.AddEditModules(Id, modules);

                    }
                    string sResponse = (roles.role_id > 0) ? "Update" : "Insert";
                    context.Request.ContentType = "text/plain";
                    context.Response.Write(sResponse);

                    
                    
                }

                string view_role = context.Request["view_role"];
                if (!string.IsNullOrEmpty(view_role))
                {
                    RenderRoles(context);
                }

                string delete_roles = context.Request["delete_roles"];
                if (!string.IsNullOrEmpty(delete_roles))
                {
                    List<int> list = serializer.Deserialize<List<int>>(delete_roles);
                    MembershipData.DeleteRoles(list);
                    RenderRoles(context);
                }
                #endregion

                #region Users
                string post_users = context.Request["post_users"];
                if (!string.IsNullOrEmpty(post_users))
                {
                    core_user users = serializer.Deserialize<core_user>(post_users);
                    users.role_id = (users.role_id.Value > 0) ? users.role_id : null;
                    string sResponse = MembershipData.InsertOrUpdateUsers(users);
                    context.Request.ContentType = "text/plain";
                    context.Response.Write(sResponse);
                }                

                string view_user = context.Request["view_user"];
                if (!string.IsNullOrEmpty(view_user))
                {
                    RenderUsers(context);
                }


                string lock_user = context.Request["lock_user"];
                if (!string.IsNullOrEmpty(lock_user))
                {
                    List<int> list = serializer.Deserialize<List<int>>(lock_user);
                    MembershipData.LockUnlockUsers(list);
                    RenderUsers(context);
                                
                }

                string delete_users = context.Request["delete_users"];
                if (!string.IsNullOrEmpty(delete_users))
                {
                    List<int> list = serializer.Deserialize<List<int>>(delete_users);
                    MembershipData.DeleteUsers(list);

                    

                    RenderUsers(context);
                }


                #endregion

               
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
                throw;
            }
        }

        private void RenderRoles(HttpContext context) {
            string json = HelperData.ToJSONString(MembershipData.ViewRoles());
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
            
        }

        private void RenderUsers(HttpContext context) {
            List<Dictionary<string, object>> list_user = HelperData.DataTableToDictionaries(MembershipData.ViewUsers());
            List<Dictionary<string, object>> list_role = HelperData.DataTableToDictionaries(MembershipData.ViewRoles());

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("roles", list_role);
            dic.Add("users", list_user);

            string json = HelperData.ToJSONString(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }     

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
   
   
}